<?php

namespace app\sales\controller;

use app\admin\common\Base;
use think\Db;
use app\scm\model\Invoice as Model;
//销售开票
class Salesbilling extends Base
{
   public function index()
   {
      $data = $this->request->param();

      if (isset($data['ajax']) && $data['ajax'] == 100) {
         $map = '';
         $page = isset($data['page']) ? $data['page'] : 1;
         $page = intval($page);
         $limit = isset($data['rows']) ? $data['rows'] : 10;
         $limit = intval($limit);
         $start = $limit * ($page - 1);


         if ($map == '') {
            $list = Db::name('invoice')->where('is_del', 0)->where('type', 1)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
            $count = Db::name('invoice')->where('enterprise_id', $this->EnterpriseId)->where('type', 1)->where('is_del', 0)->count();
         } else {
            $list = Db::name('invoice')->where('is_del', 0)->order('id asc')->where('type', 1)->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
            $count = Db::name('invoice')->where('enterprise_id', $this->EnterpriseId)->where('type', 1)->where('is_del', 0)->count();
         }

         foreach ($list as $key => $value) {
            $order = Db::name('order')->where('id', $value['pid'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->find();
            $list[$key]['order_addtime'] = date('Y-m-d', $order['addtime']);
            $list[$key]['customer'] = Db::name('customer')->where('id', $order['user_id'])->value('name');
            $list[$key]['sn'] = $order['sn'];
            $list[$key]['yh_price'] = $order['yh_price'];
            $order_goods = Db::name('order_good')->where('order_id', $order['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
            
            $list[$key]['tax_e'] = 0;
            $list[$key]['total_price'] = 0;
            foreach ($order_goods as $k => $v) {
               $list[$key]['tax_e'] += $v['tax_e'];
               $list[$key]['total_price'] += $v['total_price'];
            }
            $list[$key]['addtime'] = date('Y-m-d', $value['addtime']);
            switch ($order['payment_status']) {
               case 0:
                  $list[$key]['payment_status'] = '部分收款';
                  break;
               case 1:
                  $list[$key]['payment_status'] = '全部收款';
                  break;
               case 2:
                  $list[$key]['payment_status'] = '部分退款';
                  break;
               case 3:
                  $list[$key]['payment_status'] = '全部退款';
                  break;
               default:
                  # code...
                  break;
            }
         }

         return $this->grid_data($list, $count, $page, $limit);
      }
      return $this->fetch();
   }


   public function add()
   {
      $data = $this->request->param();

      if (isset($data['ajax']) && $data['ajax'] == 100) {
         $map = '';
         $page = isset($data['page']) ? $data['page'] : 1;
         $page = intval($page);
         $limit = isset($data['rows']) ? $data['rows'] : 10;
         $limit = intval($limit);
         $start = $limit * ($page - 1);

         // $sql = Db::name('invpu_goods')->alias('a')->join('invpu b', 'a.invpu_id = b.id')->where('b.addtime', '>=', strtotime($data['startdate']))->where('b.addtime', '<=', strtotime($data['enddate']))->where(['b.is_del' => 0, 'b.enterprise_id' => $this->EnterpriseId]);
         $sql = Db::name('order')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId]);
         $sql = $data['supplierid'] == '' ? $sql : $sql->where('user_id', $data['supplierid']);
         $list = $sql->select();
         $count = $sql->count();

         foreach ($list as $key => $value) {
            $list[$key]['dj_time'] = date('Y-m-d', $value['dj_time']);
            $list[$key]['supplier_name'] = Db::name('customer')->where('id', $value['user_id'])->value('name');
            switch ($value['payment_status']) {
               case 0:
                  $list[$key]['payment_status'] = '部分收款';
                  break;
               case 1:
                  $list[$key]['payment_status'] = '全部收款';
                  break;
               case 2:
                  $list[$key]['payment_status'] = '部分退款';
                  break;
               case 3:
                  $list[$key]['payment_status'] = '全部退款';
                  break;
               default:
                  # code...
                  break;
            }

            $invoice = Db::name('invoice')->where('type', 1)->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->where('pid', $value['id'])->select();
            $y_invoice_price = 0;
            if ($invoice) {
               foreach ($invoice as $k => $v) {
                  $y_invoice_price += $v['invoice_price'];
               }
            }
            $list[$key]['y_invoice_price'] = $y_invoice_price;
            $list[$key]['w_invoice_price'] = $value['yh_price'] - $y_invoice_price;

            $order_good = Db::name('order_good')->where('order_id', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
            $list[$key]['tax_e'] = 0;
            $list[$key]['number'] = 0;
            foreach ($order_good as $k => $v) {
               $list[$key]['tax_e'] += $v['tax_e'];
               // $list[$key]['number'] += $v['num'];
            }
         }
         return $this->grid_data($list, $count, $page, $limit);
      }

      return $this->fetch();
   }

   public function save()
   {
      $status = 0;
      $msg = "操作失败";
      $data = $this->request->param();
      $arr = [];
      foreach ($data['movedata'] as $key => $value) {
         $arr[$key]['pid'] = $value['id'];
         $arr[$key]['addtime'] = time();
         $arr[$key]['invoice_no'] = $data['invoice_no'];
         $arr[$key]['invoice_price'] = $value['invoice_price'];
         $arr[$key]['invoice_title'] = $data['invoice_title'];
         $arr[$key]['enterprise_id'] = $this->EnterpriseId;
         $arr[$key]['type'] = 1;
      }

      $model = new Model();

      $result = $model->allowField(true)->saveAll($arr);
      if ($result) {
         return json(['status' => 1, 'msg' => '操作成功']);
      } else {
         return json(['status' => $status, 'msg' => $msg]);
      }
   }
}
